package com.dumas.controller;

import com.dumas.pojo.Books;
import com.dumas.service.BookService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;

@Controller
@RequestMapping("/book")
public class BookController {

    @Autowired
    @Qualifier("bookServiceImpl")
    private BookService bookService;

    @RequestMapping("/allBook")
    public String allBook(Model model){     //查询全部图书
        List<Books> booksList = bookService.queryAllBooks();
        model.addAttribute("bookList", booksList);
        return "allBook";
    }

    @RequestMapping("/toAddBook")
    public String toAddBook(){      //跳转到addBook页面
        return "addBook";
    }

    @RequestMapping("/addBook")
    public String addBook(Books books){     //添加图书
        bookService.addBooks(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/toUpdateBook")
    public String toUpdateBook(int id, Model model){        //跳转到toUpdateBook页面
        Books books = bookService.queryBooksById(id);
        model.addAttribute("book", books);
        return "updateBook";
    }

    @RequestMapping("/updateBook")
    public String updateBook(Books books){      //更新图书
        bookService.updateBooks(books);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/deleteBook")
    public String deleteBook(int id){      //删除图书
        bookService.deleteBooks(id);
        return "redirect:/book/allBook";
    }

    @RequestMapping("/queryBook")
    public String queryBook(String bookName, Model model){      //搜索图书
        List<Books> booksList = bookService.queryBooksByName(bookName);
        model.addAttribute("bookList", booksList);
        return "queryBook";
    }
}
